# coding: UTF-8 -*-
import httplib
import sys
import StringIO

HOST = "localhost:8080"
POST_REQUEST = "/post"

def doPost(xml):
    webservice = httplib.HTTP(HOST)
    webservice.putrequest("POST", POST_REQUEST)
    webservice.putheader("Host", HOST)
    webservice.putheader("User-Agent", "Python post")
    webservice.putheader("Content-type", "application/xml; charset=\"UTF-8\"")
    webservice.putheader("Content-length", "%d" % len(xml))
    #webservice.putheader("SOAPAction", "\"\"")
    webservice.endheaders()
    webservice.send(xml)
    
    # get the response

    statuscode, statusmessage, header = webservice.getreply()
    print "Response: ", statuscode, statusmessage
    print "headers: ", header
    res = webservice.getfile().read()
    print res

def main():
    if len(sys.argv) < 2:
        print "Error: parsing command line arguments"
        print "usage: python %s <xml-file>" % __file__
        sys.exit(1)

    xmlfile = sys.argv[1]
    #xml = open(xmlfile, 'r').read().encode("utf-8");
    xmlStr = StringIO.StringIO(open(xmlfile, 'rt').read()); 
    doPost(xmlStr.getvalue()) 


if __name__ == '__main__':
    main()
